<template>
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%;">
    <el-col>
      <span>{{ treeNode.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="handleCommand">
            <span>
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from '@/api/departments' // 删除部门的接口函数
// 该组件需要对外开放属性 外部需要提供一个对象 对象里需要有name  manager
export default {
  props: {
    treeNode: { // 当前点击的部门信息
      type: Object, // 对象类型
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
  // 处理下拉菜单的点击
    handleCommand(command) { // 组件里面自带的触发方法
      // 添加部门
      if (command === 'add') {
        this.$emit('addDepts', this.treeNode)
      }
      // 编辑部门
      if (command === 'edit') {
        this.$emit('editDepts', this.treeNode)
      }
      // 删除部门
      if (command === 'del') {
        this.$confirm('确定解散该部门, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          await delDepartments(this.treeNode.id) // 删除当前点击的部门
          this.$emit('del-dept') // 通知父组件重新拉取数据
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    }
  }
}
</script>

<style>

</style>
